﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Guedes_Library;
using AdmBase;
using System.Text;

public partial class admin_pages_banner_Novo : TelaBase
{


    protected void Page_Load(object sender, EventArgs e)
    {
        IsLogado();
        File = fileUpload;
        string id = Request.QueryString["id"];
        if (id != null && !IsPostBack)
        {
            banner b = BBusiness.GetById(int.Parse(id));
            if (b == null)
                Response.Redirect("~/admin/pages/banner/Default.aspx");
            carregaAlterar(b);
        }
    }


    public void carregaAlterar(banner b)
    {
        txtDescricao.Text = b.Desc;
        txtNome.Text = b.Name;
        hdId.Value = b.ID.ToString();
        hdFoto.Value = b.Img_Banner;
        img.ImageUrl = ResolveClientUrl("~/File/thumb/"+b.Img_Banner);

    }

    private BannerBusiness bBusiness;

    public BannerBusiness BBusiness
    {
        get
        {
            if (bBusiness == null)
                bBusiness = new BannerBusiness();
            return bBusiness;
        }
        set { bBusiness = value; }
    }

    protected void Salvar(object sender, EventArgs e)
    {
        banner b = new banner();

        if (hdId.Value != "")
            b.ID = int.Parse(hdId.Value);
        b.Name = txtNome.Text;
        b.Img_Banner = hdFoto.Value;
        b.Desc = txtDescricao.Text;

        Retorno retorno = BBusiness.Salvar(b);
        if (!retorno.Error)
            Limpar();
        ShowMensagem("Atenção", retorno.Mensagem);
    }


    public void Limpar()
    {
        txtDescricao.Text = String.Empty;
        txtNome.Text = String.Empty;
        hdId.Value = String.Empty;
        img.ImageUrl = "~/admin/Images/pimenta.png";
    }

    public override void DoReturnPage()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("top.$get(\"" + img.ClientID + "\").src = '" + ResolveClientUrl("~/File/thumb/" + UrlArquivo) + "';");
        sb.Append("top.$get(\"" + hdFoto.ClientID + "\").value = '" + UrlArquivo + "';");
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", sb.ToString(), true);
    }

}